<!DOCTYPE html>
<#assign secSelect = JspTaglibs["http://www.casic304.cn/secSelect"] />
<html>
<head>
    <meta charset="utf-8">
    <title>消息发送</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
    <link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
    <link rel="stylesheet" href="${ctx}/css/public.css">
    <style>
        .file-name,.deleteFile{
            cursor:pointer
        }
    </style>
    <script type="text/javascript">
        var __ctx = "${ctx}";
    </script>
</head>
<body>

<div style="padding: 10px;position: absolute;left: 0;overflow: auto;top: 0; bottom: 0;right: 0;height: 90%;">
    <form class="layui-form sendViewForm" lay-filter="sendViewForm"
          style="margin:1px 1px 1px 1px;padding:20px 28px 20px 0;background: #fff">
        <div class="layui-form-item">
            <label class="layui-form-label">标题:<label style="color: red;">*</label></label>
            <div class="layui-input-block" >
                <div  style="width:50%;display: inline-block">
                    <input type="text"  name="subject" lay-verify="subject" autocomplete="off"
                           class="layui-input" value="${messageSend.subject}">
                </div>
            </div>
        </div>
        <div class="layui-form-item">

            <label class="layui-form-label">收信人:</label>
            <div class="layui-input-block" >
                <div  style="width:50%;display: inline-block;float: left">
                    <input type="hidden" name="receiverId"  value="${messageSend.receiverId}">
                    <input   readonly="readonly" type="text" name="receiverName"  lay-verify="receiverName" autocomplete="off"
                             class="layui-input"  value="${messageSend.receiverName}">
                </div>
                <button class="layui-btn selectUser" inputIndex="1" type="button" style="float:left;margin-left: 10px">选择</button>
                <button class="layui-btn cleanUser" type="button" style="float: left">清空</button>
            </div>
        </div>

        <div class="layui-form-item">

            <label class="layui-form-label">收信组织:</label>
            <div class="layui-input-block">
                <div style="width:50%;display: inline-block;float: left">
                    <input type="hidden" name="receiverOrgId"  value="${messageSend.receiverOrgId}">
                    <input type="text"  readonly="readonly" name="receiverOrgName" lay-verify="receiverOrgName" autocomplete="off"
                           value="${messageSend.receiverOrgName}"class="layui-input">
                </div>
                <button class="layui-btn selectOrg" inputIndex="1" type="button" style="float: left;margin-left: 10px">选择</button>
                <button class="layui-btn cleanOrg" type="button" style="float: left">清空</button>

            </div>
        </div>


        <!--<div class="layui-form-item">
            <label class="layui-form-label">附件:</label>
            <div class="layui-input-block">

                <input type="text" name="attachment" lay-verify="attachment" autocomplete="off"
                    class="layui-input"  value="${messageSend.attachment}">

            </div>
        </div>-->

        <div class="layui-form-item">
            <label class="layui-form-label">需要回复:</label>
            <div class="layui-input-block">
                <input type="radio" name="canReply" value="1" title="是" checked/>
                <input type="radio" name="canReply" value="0" title="否" <#if messageSend.canReply==0>checked</#if> />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">附件上传:</label>
            <div class="layui-input-block">

                <div class="uploadFileCont">
                    <div style="margin-right: 10px;width: 15%;float: left;" id="uploadLevel">
                        <@secSelect.sec_select data_src=1 name="fileLevel"/>
                    </div>
                    <button type="button" class="layui-btn layui-btn-normal" id="testList" style="float:left">附件上传</button>
                    <div style="clear: both;"></div>
                    <input type="file" id="xmlFile" name="xmlFile" style="display: none!important" multiple/>
                    <div class="uploadFileList" style="padding-top: 9px;"></div>

                </div>

            </div>
            <!--<input type="text" name="jsonFile" id="jsonFile" value='${messageSend.jsonFile}' style="display: none;"/>-->

        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">内容:</label>
            <div class="layui-input-block">
                <a style="display: none" class="textareaHref" target="_blank"><span class="textareaHrefClick"></span></a>
                <textarea style="height:50px" id="content" name="content" id="messageUrl" style="display:none"class="layui-textarea"> ${messageSend.content}</textarea>
            </div>
        </div>
        <a style="display: none" class="textareaHref" target="_blank"><span class="textareaHrefClick"></span></a>

        <div class="layui-form-item" style="text-align:center">
            <div class="layui-input-block" style="text-align: center;margin: 0">
                <button class="layui-btn add" lay-submit lay-filter="add" type="submit">发送</button>
                <a class="layui-btn add" href="${ctx}/system/messageSend/view/form">返回</a>
            </div>
            <input type="hidden" name="id"  value="${messageSend.id}"/>
            <input type="hidden" name="jsonFile1" value='${messageSend.jsonFile}'/>
            <input type="hidden" name="messageType" value="1"/>

        </div>
    </form>
</div>

<script src="${ctx}/public/component.js" type="text/javascript"></script>
<script src="${ctx}/layui/layui.js"></script>
<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table', 'layer', 'setter', 'commons', 'common', 'totalFilter', 'form', 'jquery', 'upload', 'layedit'], function () {
        var table = layui.table, layer = layui.layer, setter = layui.setter, commons = layui.commons, common = layui.common, totalFilter = layui.totalFilter, form = layui.form, $ = layui.jquery;var layedit = layui.layedit,upload = layui.upload;
        //初始化富文本剪辑器
        var index = layedit.build('content',{
            tool: ['strong' //加粗
                ,'italic' //斜体
                ,'underline' //下划线
                ,'del' //删除线
                ,'|' //分割线
                ,'left' //左对齐
                ,'center' //居中对齐
                ,'right' //右对齐
                ,'face' //表情
            ]
        });

        //下载附件
        window.downFile = function (obj){
            var fileId =  $(obj).parents("span").attr("fileId");
            window.location.href = serverPath.formPath + setter.sysLinkPath + "attachFile/download?fileId="+fileId;
        }
        var newStr = "";

        var uploadInst = upload.render({
            elem: '#testList' //绑定元素
            ,url: serverPath.systemPath + setter.sysLinkPath +'attachFile/fileUpload' //上传接口
            ,multiple: true
            ,accept: 'file'
            ,auto: true
            ,number: 0
            ,before: function (obj) {//附件密级的值传到后台接口
                this.data = {"fileLevel":$("select[name='fileLevel']").val()};
            }
            ,done: function(res){
                if (res.code === 0 && res.data && res.data.fileList.length > 0) {
                    var fileOne = res.data.fileList[0],
                        oneFile = '<span class="file-id" style="margin-right: 20px;" fileId=' + fileOne.fileId + '><span class="file-name" onclick="downFile(this)">' + fileOne.fileName + '.' + fileOne.extName + '</span>' +
                            '<i class="layui-icon layui-icon-delete deleteFile" ></i></span>';
                    newStr = newStr + '{'+'"fileId"'+':'+'"'+res.data.fileList[0].fileId+'"'+','+'"fileName"'+':'+'"'+res.data.fileList[0].fileName+'.'+res.data.fileList[0].extName+'"'+'},';
                    $('.uploadFileList').append(oneFile);

                }
            }
            ,error: function(){
            }
        });

        form.verify({
            subject: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (value == null || value == "") {
                    return '标题不能为空';
                }

            },
            receiverName: function (value, item) { //value：表单的值、item：表单的DOM对象
                if ((value == null || value == "")&&($("input[name='receiverOrgName']").val()==null||$("input[name='receiverOrgName']").val()=="")) {
                    return '收信人或收信组织不能为空';
                }
            }
        });

        var jsonFile = $("[name='jsonFile1']").val();

        var files = eval(jsonFile);
        if(files){
            for(var i=0;i<files.length;i++){
                newStr = '<span class="file-id" style="margin-right: 20px;" fileId=' + files[i].fileId + '><span class="file-name" onclick="downFile(this)">' + files[i].fileName + '</span><i class="layui-icon layui-icon-delete deleteFile" ></i></span>';
                $('.uploadFileList').append(newStr);
            }
        }

        // 删除
        $(document).on('click', '.deleteFile', function () {
            $(this).parent().remove();
        });

        //表单渲染
        var indexOne = parent.layer.getFrameIndex(window.name);
        form.render();

        //监听表单提交
        form.on('submit(add)', function (data) {
            var files = $(".uploadFileList .file-id");
            var fileList = [];
            var lasd = "";
            $.each(files,function (i, value) {
                var fileId = $(this).attr("fileid");
                var fileName = $(this).find("span").html();
                lasd += '{"fileId":"'+fileId+'","fileName":"'+fileName+'"},';
            })
            lasd = lasd.substring(0,lasd.length-1);
            lasd = '['+lasd+']';
            data.field.jsonFile = lasd;
            data.field.content = layedit.getContent(index);
            $.ajax({
                url: serverPath.systemPath + setter.sysLinkPath + "messageSend/add",
                type: "POST",
                contentType: 'application/json',
                dataType: 'json',
                data: JSON.stringify(data.field),
                success: function (data) {
                    if (commons._filterRecord(data)) {
                        layer.alert(data.msg,{skin: 'btn-class'}, function (index) {
                            $(top.document).find("iframe[src='${ctx}/system/messageSend/view/form']").attr("src",'/system/messageSend/view/form');     //刷新已发送列表页面
                            setTimeout(function () {
                                window.parent.$("a[lay-href='${ctx}/system/messageSend/view/form']").click();
                            },0);
                            window.layer.close(index);//跳转到发送列表页
                        });
                    } else {
                        layer.alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("保存失败！" + XMLHttpRequest.status);
                }
            });
            return false;
        });

        $('.layui-layedit-iframe').find('iframe').contents().find('a').css({'cursor':'pointer'});
        $('.layui-layedit-iframe').find('iframe').contents().find('a').click(function () {
            $('.textareaHref').attr('href',$(this).attr('href'));
            $('.textareaHrefClick').click()
        });

        //人员多选选择器
        layui.use(['layer', 'setter'], function () {
            var setter = layui.setter
                , layer = layui.layer;
            var userSelectorWin;
            $('.selectUser').click(function () {
                var theData = $(this).siblings("div").find("input[name=receiverName]");
                var hiddenData = $(this).siblings("div").find("input[name=receiverId]");
                var body;
                layer.open({
                    type: 2
                    , title: '人员选择器'
                    , btn:["保存","取消","撤销"]
                    , area: ['980px', '630px']
                    , resize: false
                    , content: serverPath.systemPath + setter.sysLinkPath+'dialog/view/sysUserDialog'
                    , success: function (layero, index) {
                        body = layer.getChildFrame('body',index);
                        userSelectorWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        //向用户选择器页面传递初始值[{name:"name1",userId:"userId1"},{name:"name2",userId:"userId2"}...]
                        if (theData.val()) {
                            userSelectorWin.layui.userList = [];
                            var hiddenDataArr = hiddenData.val().split(",");
                            var theDataArr = theData.val().split(",");
                            $.each(hiddenDataArr,function (k,v) {
                                var userListObj={};
                                userListObj.name = theDataArr[k];
                                userListObj.userId = v;
                                userSelectorWin.layui.userList.push(userListObj);
                            })
                        }
                    }
                    ,yes:function(index,layero){
                        //用户选择器返回的选择人员数组[{name:"name1",userId:"userId1"},{name:"name2",userId:"userId2"}...]
                        var theUserList = body.find("input[name=userList]").val();
                        if (theUserList) {
                            dateReturn(JSON.parse(theUserList));
                        }else {
                            $("input[name='receiverName']").attr("value", '');
                            $("input[name='receiverId']").attr("value", '');
                        }

                        layer.close(index);
                    },
                    btn3:function(index, layero){
                        userSelectorWin.cancelData(index);
                        return false //开启该代码可禁止点击该按钮关闭
                    }
                });
            });
            // 给“人员选择器”初始化userList
            function initUserList(){
                return $.map(userSelectorWin.ownerList,function(v){
                    return {name:v.ownerName,userId:v.ownerId};
                })
            }
        });
//组织多选选择器
        layui.use(['layer', 'setter'], function () {
            var setter = layui.setter
                , layer = layui.layer;
            $('.selectOrg').click(function () {
                var theData = $(this).siblings("div").find("input[name=receiverOrgName]");
                var hiddenData = $(this).siblings("div").find("input[name=receiverOrgId]");
                var orgSelectorWin;
                var body;
                layer.open({
                    type: 2
                    , title: '组织选择器'
                    , btn:["保存","取消","撤销"]
                    , area: ['980px', '630px']
                    , resize: false
                    , content: serverPath.systemPath + setter.sysLinkPath+'dialog/view/sysOrgDialog'
                    , success: function (layero, index) {
                        body = layer.getChildFrame('body',index);
                        orgSelectorWin = window[layero.find('iframe')[0]['name']];
                        //向用户选择器页面传递初始值[{name:"name1",userId:"userId1"},{name:"name2",userId:"userId2"}...]
                        if (theData.val()) {
                            orgSelectorWin.layui.orgList = [];
                            var hiddenDataArr = hiddenData.val().split(",");
                            var theDataArr = theData.val().split(",");
                            $.each(hiddenDataArr,function (k,v) {
                                var userListObj={};
                                userListObj.name = theDataArr[k];
                                userListObj.orgId = v;
                                orgSelectorWin.layui.orgList.push(userListObj);
                            })
                        }
//                        name: "wang2"
//​
//userId: "55a517a53e9e441ba8885b3369fa6fe4"
                    },
                    yes: function(index, layero){
                        //用户选择器返回的选择人员数组[{name:"name1",userId:"userId1"},{name:"name2",userId:"userId2"}...]
                        var theOrgList = body.find("input[name=orgList]").val();
                        if (theOrgList){
                            orgDateReturn(JSON.parse(theOrgList));
                        }else {
                            $("input[name='receiverOrgName']").attr("value", '');
                            $("input[name='receiverOrgId']").attr("value", '');
                        }

                        layer.close(index);
                    },
                    btn3:function(index, layero){
                        orgSelectorWin.cancelData(index);
                        return false //开启该代码可禁止点击该按钮关闭
                    }
                });
            })
        });
        //组织选择器值回填
        function orgDateReturn(obj) {
            var receiverOrgName = '';
            var receiverOrgId = '';
            $.each(obj,function (c,val) {
                if (c == 0) {
                    receiverOrgId = obj[c].orgId;
                    receiverOrgName = obj[c].name;
                } else {
                    receiverOrgName = receiverOrgName + ',' + obj[c].name;
                    receiverOrgId=  receiverOrgId + ',' + obj[c].orgId;
                }
            });
            $("input[name='receiverOrgName']").attr("value", receiverOrgName);
            $("input[name='receiverOrgId']").attr("value", receiverOrgId);
        }
        //人员选择器值回填
        function dateReturn(obj) {
            var receiverName = '';
            var receierId = '';
            $.each(obj,function (c,val) {
                if (c == 0) {
                    receierId = obj[c].userId;
                    receiverName = obj[c].name;
                } else {
                    receiverName = receiverName + ',' + obj[c].name;
                    receierId = receierId + ',' + obj[c].userId;
                }
            })
            $("input[name='receiverName']").attr("value", receiverName);
            $("input[name='receiverId']").attr("value", receierId);
        }
        //清空人员选择
        $(".cleanUser").click(function () {
            $("input[name='receiverName']").attr("value", '');
            $("input[name='receiverId']").attr("value", '');
        })
        //清空组织选择
        $(".cleanOrg").click(function () {
            $("input[name='receiverOrgName']").attr("value", '');
            $("input[name='receiverOrgId']").attr("value", '');
        })
        $(".cancelView").click(function () {
            parent.layer.close(indexOne);
        })

    });
</script>
</body>
</html>